package graphic.render;

import java.util.Random;

import game.Entity2D;
import game.SheetBasedAnimatedEntity2D;
import graphic.drawables.Sheet;
import graphic.drawables.Sprite;


/**
 * Specializzazione di {@link Render2D} ottimizzata per il disegno di singoli sprite 2D.
 * @see Render2D
 * @author Jan
 *
 */
public class StandardRender extends Render2D{
	
	private Sprite sprite;
	private Sheet sheet;
	
	
	private void init(Entity2D entity) {
		if(entity.useSheet()){
			SheetBasedAnimatedEntity2D sheetedEntity = ((SheetBasedAnimatedEntity2D)entity);
			entityPixles = sheetedEntity.getFrame();
			coo = sheetedEntity.getCoordinate();
			
			width = sheetedEntity.getSheet().getWidth();
			height = sheetedEntity.getSheet().getHeight();
			
		}else{
			sprite = entity.getSprite();
			coo = entity.getCoordinate();
			entityPixles = sprite.getSpritePixels();
			width = sprite.getWidth();
			height = sprite.getHeight();
		}
		
		xPos = coo.getX();
		yPos = coo.getY();
		
		xCentre = xPos - ( width >> 1 );
		yCentre = yPos - ( height >> 1 );
	}

	
	@Override
	public void render(Entity2D entity) {
		
		init(entity);
		if( entityInsideOfTheRenderArea(false) ){
		
	    	for (int yOffset = 0; yOffset < height; yOffset++) {
				for (int xOffset = 0; xOffset < width; xOffset++) {
					
					pixelX = xPos+xOffset;
					pixelY = yPos+yOffset;
					
					if(pixelInsideOfTheRenderArea(pixelX,pixelY)){
						pixelColor = entityPixles[xOffset + yOffset*width];
						renderAreaPixels[pixelX + (pixelY)*renderAreaWidth] = pixelColor;
					}
					
				}
			}
		}
	}
	
	@Override
	public void render(Entity2D[] entities) {
		for (Entity2D entity : entities) {
			if (entity != null) {
				render(entity);
			}
		}
	}
	
	
	@Override
	public void centredRender(Entity2D entity) {
		
		init(entity);
		if( entityInsideOfTheRenderArea(true) ){
		
	    	for (int yOffset = 0; yOffset < height; yOffset++) {
				for (int xOffset = 0; xOffset < width; xOffset++) {
					
					pixelX = xCentre+xOffset;
					pixelY = yCentre+yOffset;
					
					if( pixelInsideOfTheRenderArea(pixelX,pixelY) ){
						pixelColor = entityPixles[xOffset + yOffset*width];
						//recupero l'alpha e verifico che ci sia bisogno di disegnare il pixel o meno.
						//se disegno un pixel trasparente, sovrascrivo quelli "visibili" che sono stati disegnati da altri!
						//in questo caso elimino del tutto le trasparenze ... 
						//Per i curiosi .. provare == invece di != ;)
						if( (pixelColor>>24) < 0  ){
							renderAreaPixels[pixelX + (pixelY)*renderAreaWidth] = pixelColor;
	   					}
						
					}
				}
			}
		}
	}
	
	@Override
	public void centredRender(Entity2D[] entities) {
		for (Entity2D entity : entities) {
			if (entity != null) {
				centredRender(entity);
			}
		}
	}

	@Override
	public void monoChromaticCentredRender(int whatColor, Entity2D entity) {
		
		init(entity);
		
		if( entityInsideOfTheRenderArea(true) ){
		
		
	    	for (int yOffset = 0; yOffset < height; yOffset++) {
				for (int xOffset = 0; xOffset < width; xOffset++) {
					
					pixelX = xCentre+xOffset;
					pixelY = yCentre+yOffset;
					
					if( pixelInsideOfTheRenderArea(pixelX,pixelY) ){
	   					pixelColor = entityPixles[xOffset + yOffset*width];
	   					
						switch (whatColor) {
						case ONLY_RED:		pixelColor = pixelColor & 0xFFFF0000;
											break;
											
						case ONLY_GREEN:	pixelColor = pixelColor & 0xFF00FF00;
											break;
											
						case ONLY_BlUE:		pixelColor = pixelColor & 0xFF0000FF;
											break;
											
						default:			return;
						}
						
						renderAreaPixels[pixelX + (pixelY)*renderAreaWidth] = pixelColor;
					}
				}
			}
		}
	}
	
	@Override
	public void monoChromaticCentredRender(int whatColor, Entity2D[] entities) {
		for (Entity2D entity : entities) {
			if (entity != null) {
				monoChromaticCentredRender(whatColor, entity);
			}
		}
	}
}
